package com.salud.mendoza;

import au.com.bytecode.opencsv.CSV;
import au.com.bytecode.opencsv.CSVReadProc;
import au.com.bytecode.opencsv.CSVWriteProc;
import au.com.bytecode.opencsv.CSVWriter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by ricardo on 09/06/14.
 */
public class CSVManager {
    private static CSVManager csvManager;
    private CSV csvWriter;
    private CSV csvReader;
    private List<String[]> valoresALeer;

    private CSVManager() {
        csvReader = CSV.separator('\t').create();
        csvWriter = CSV.separator(' ').noQuote().create();

    }

    public static CSVManager getInstance() {
        if (csvManager == null) {
            csvManager = new CSVManager();
        }
        return csvManager;
    }

    public List<String[]> readFile(String archivo) {
        valoresALeer = new ArrayList<String[]>();
        csvReader.read(archivo, new CSVReadProc() {
            @Override
            public void procRow(int i, String... values) {
                valoresALeer.add((String[]) Arrays.asList(values).toArray());
            }
        });
        return valoresALeer;
    }


    public void writeFile(String archivo, final List<String[]> valoresAEscribir) {
        csvWriter.write(archivo, new CSVWriteProc() {
            @Override
            public void process(CSVWriter csvWriter) {
                csvWriter.writeAll(valoresAEscribir);
            }
        });
    }

}
